/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import dao.DepartmentDAO;
import dao.DesignationDAO;
import dao.SectionDAO;
import dao.VacanciesDAO;
import data.Departments;
import data.Designation;
import data.Sections;
import data.Vacancies;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.table.TableModel;
import org.hibernate.HibernateException;
import view.ManageVacancy;

/**
 *
 * @author Hùng
 */
public class VacanciesBO {

    private String Success;

    public VacanciesBO() {
    }

    public String getSuccess() {
        return Success;
    }

    public void setSuccess(String Success) {
        this.Success = Success;
    }

    public VacanciesBO(String Success) {
        this.Success = Success;
    }

    public List<Vacancies> showAllInformationVacancies() {
        List<Vacancies> lVac = new VacanciesDAO().getallVancanies();
        return lVac;
    }
    public List<Vacancies> showAllInforVacanciesisNeeding() {
        List<Vacancies> lVac = new VacanciesDAO().getallVancaniesbyNeeding();
        return lVac;
    }

    public void registerVacancy(Object tmpdeptname, Object tmpsecName, Object tmpdesign, String tmpNumber, Date tmpdate, String prioriti, String tmpflag) {
        if (tmpdeptname != null && tmpdesign != null && !tmpNumber.equals("") && tmpdate != null && !prioriti.equals("") ) {
            //check setion
            if ((tmpflag.equals("0") && tmpsecName == null) || (!tmpflag.equals("0") && tmpsecName != null)) {
                //check number of vacancies
                int number = 0;
                try {
                    number = Integer.parseInt(tmpNumber);
                } catch (NumberFormatException e) {
                    throw new Error("Please,check number of vacanies.Because You " + e.getMessage());
                }
                String deptname = (String) tmpdeptname;
                Departments tmpdept = new DepartmentDAO().getDepartmentByName(deptname);
                String design = (String) tmpdesign;
                Designation tmpdesig = new DesignationDAO().getDesignationbyDesignation(design);
                Sections tmpSec = null;
                if (tmpsecName != null) {
                    String nameSec = (String) tmpsecName;
                    tmpSec = new SectionDAO().getSectionbyNameSec(nameSec);
                }
//                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
//                String date = df.format(tmpdate);
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                String date = format1.format(tmpdate);
                Vacancies tmpVac = new Vacancies(
                        "V" + Calendar.getInstance().getTimeInMillis(),
                        tmpdept,
                        tmpdesig,
                        tmpSec,
                        number,
                        "Needing",
                        date,
                        prioriti,
                        null);
                boolean flag = new VacanciesDAO().executeInsertHQLQuery(tmpVac);
                if (flag) {
                    Success = "Register Vacancy SuccessFully";
                } else {
                    throw new Error("Register Vacancy Fail" + date);
                }
            } else {
                throw new Error("Please,check your Section");
            }
        } else {
            throw new Error("Please,check your information");
        }
    }

    public void removeVacancy(int[] selectedRows, TableModel model) {
        if (selectedRows.length > 0) {
            try {
                List<Vacancies> lVac = new ArrayList<Vacancies>();
                for (int i = 0; i < selectedRows.length; i++) {
                    String tmpIdVac = (String) model.getValueAt(selectedRows[i], 0);
                    Vacancies tmpVac = new VacanciesDAO().getVacancybyId(tmpIdVac);
                    if (tmpVac.getVacancyfilldetailses().size() > 0) {
                        throw new Error("Delete Vacancy" + tmpIdVac + "  Fail.Please,Delete List Vacancy fill details of Vacancy");
                    }
                    lVac.add(tmpVac);
                }
                for (Vacancies a : lVac) {
                    new VacanciesDAO().executeDeleteHQLQuery(a);
                }
                Success = "Delete  SuccessFully";
            } catch (HibernateException he) {
                throw new Error("Delete Fail." + he.getMessage());
            }
        } else {
            throw new Error("Please,Select Rows on Table to delete");
        }
    }

    public void showInforOfVac(int[] selectedRows, TableModel model) {
        if (selectedRows.length == 1) {
            String tmpIdVac = (String) model.getValueAt(selectedRows[0], 0);
            Vacancies tmpVac = new VacanciesDAO().getVacancybyId(tmpIdVac);
            ManageVacancy.setsVacancies(tmpVac);
        } else {
            throw new Error("Please,Select only Rows on Table to edit");
        }

    }

    public void EditInforofVac(Object tmpDeptName, Object tmpSecName, Object tmpDesign, String tmpNumber, String Prioriti, Date tmpdate,String tmpIdVac ,String tmpflag) {
        //check infor
        if (tmpDeptName != null && tmpDesign != null && !tmpNumber.equals("") && tmpdate != null && !Prioriti.equals("") ){
             //check setion
            if ((tmpflag.equals("0") && tmpSecName == null) || (!tmpflag.equals("0") && tmpSecName != null)){
                //check number of vacancies
                int number = 0;
                try {
                    number = Integer.parseInt(tmpNumber);
                } catch (NumberFormatException e) {
                    throw new Error("Please,check number of vacanies.Because You " + e.getMessage());
                }
                String deptname = (String) tmpDeptName;
                Departments tmpdept = new DepartmentDAO().getDepartmentByName(deptname);
                String design = (String) tmpDesign;
                Designation tmpdesig = new DesignationDAO().getDesignationbyDesignation(design);
                Sections tmpSec = null;
                if (tmpSecName != null) {
                    String nameSec = (String) tmpSecName;
                    tmpSec = new SectionDAO().getSectionbyNameSec(nameSec);
                }
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                String date = format1.format(tmpdate);
                Vacancies tmpVac = new VacanciesDAO().getVacancybyId(tmpIdVac);
                tmpVac.setDepartments(tmpdept);
                tmpVac.setDesignation(tmpdesig);
                tmpVac.setSections(tmpSec);
                tmpVac.setNoofvacancies(number);
                tmpVac.setPriority(Prioriti);
                tmpVac.setVacancydate(date);
                boolean flag = new VacanciesDAO().executeUpdateHQLQuery(tmpVac);
                if(flag){
                    Success = "Edit SuccessFully ";
                }else
                {
                    throw new Error("Edit Fail");
                }
              
            }else {
                throw new Error("Please,check your Section");
            }
         }else{
             throw new Error("Please,check your information");
         }
    }
}
